//
//  WMESongListViewController.m
//  WMEMusic
//
//  Created by 恩叔 on 16/4/23.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMESongListViewController.h"
#import "WMESongListModel.h"
#import "WMESongListCell.h"
#import "WMEPlayerViewController.h"
#import "NetwrokHandler.h"
#import "WMEPlayerModel.h"
@interface WMESongListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIImageView *bottomView;
@property (nonatomic, strong)UILabel *arrow;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end
static NSInteger touchCount = 0;

@implementation WMESongListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createImg];
    [self createView];
    [self data];
}
- (void)createImg
{
    WS(ws);
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH)];
    [self.view addSubview:img];
    img.contentMode = UIViewContentModeScaleAspectFill;
    img.backgroundColor = [UIColor redColor];
    [img sd_setImageWithURL:[NSURL URLWithString:_model.pic_300]];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
        make.height.equalTo(img.mas_width);
    }];
    self.bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, img.frame.size.height, WIDTH, HEIGHT - 64 - img.frame.size.height - 40)];
    [self.view addSubview:_bottomView];
    _bottomView.backgroundColor = [UIColor orangeColor];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(img.mas_bottom).with.offset(0);
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(-40);
    }];
    //求rgb值,用来给bottomView赋值
    UIColor *rgb = [[UIColor alloc] init];
    rgb = [[[WMEPointRBGAcquire alloc] init] getPixelColorAtLocation:img.center img:img];
    _bottomView.backgroundColor = rgb;
    //给bottomView添加高斯模糊
//    UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//    visualEffectView.frame = _bottomView.bounds;
//    [_bottomView addSubview:visualEffectView];
    UIView *brack = [[UIView alloc] initWithFrame:CGRectMake(0, img.frame.size.height, WIDTH, HEIGHT - 64 - img.frame.size.height - 40)];
    [_bottomView addSubview:brack];
    brack.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [brack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(img.mas_bottom).with.offset(0);
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(-40);
    }];
    UILabel *title = [[UILabel alloc] init];
    title.numberOfLines = 0;
    title.font = [UIFont systemFontOfSize:13];
    title.textColor = [UIColor whiteColor];
    [_bottomView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bottomView.mas_left).with.offset(10 * WIDTHSCALE);
        make.top.equalTo(_bottomView.mas_top).with.offset(10 * HEIGHTSCALE);
        make.right.equalTo(_bottomView.mas_right).with.offset(-10 *WIDTHSCALE);
        make.height.equalTo(@(20 * HEIGHTSCALE));
    }];
    title.text = _model.title;
    
    UILabel *tagLabel = [[UILabel alloc] init];
    tagLabel.numberOfLines = 0;
    tagLabel.font = [UIFont systemFontOfSize:13];
    [_bottomView addSubview:tagLabel];
    tagLabel.textColor = [UIColor whiteColor];
    [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bottomView.mas_left).with.offset(10 * WIDTHSCALE);
        make.right.equalTo(_bottomView.mas_right).with.offset(-10 * WIDTHSCALE);
        make.top.equalTo(title.mas_bottom).with.offset(10 * HEIGHTSCALE);
        make.height.equalTo(@(20 *HEIGHTSCALE));
    }];
    tagLabel.text = [NSString stringWithFormat:@"标签 : %@", _model.contentTag];
    
    UILabel *desc = [[UILabel alloc] init];
    desc.font = [UIFont systemFontOfSize:13];
    desc.numberOfLines = 0;
    desc.textColor = [UIColor whiteColor];
    [_bottomView addSubview:desc];
    CGFloat gao = [WMEHeight heightWithText:[NSString stringWithFormat:@"介绍 : %@", _model.desc] FontOfSize:14 MySize:CGSizeMake(WIDTH - 20 * WIDTHSCALE, _bottomView.frame.size.height - 50 * HEIGHTSCALE)];
    
    [desc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bottomView.mas_left).with.offset(10 * WIDTHSCALE);
        make.right.equalTo(_bottomView.mas_right).with.offset(- 10 * WIDTHSCALE);
        make.top.equalTo(tagLabel.mas_bottom).with.offset(0);
//        make.bottom.equalTo(_bottomView.mas_bottom).with.offset(-10);
        make.height.equalTo(@(gao));
    }];
    desc.text = [NSString stringWithFormat:@"介绍 : %@", _model.desc];
}
- (void)createView
{
    WS(ws);
    UIView *hearView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT -(HEIGHT - (HEIGHT - HEIGHT / 5 * 3)) - 64)];
    hearView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0f];
    hearView.userInteractionEnabled = YES;
    self.arrow = [[UILabel alloc] init];
//    _arrow.frame = CGRectMake(WIDTH/ 2 - 50,HEIGHT -(HEIGHT - (HEIGHT - HEIGHT / 5 * 3)) - 64 - 20 , 100, 20);
    _arrow.text = @"↓";
    _arrow.textAlignment = NSTextAlignmentCenter;
    _arrow.userInteractionEnabled = YES;
    UITapGestureRecognizer *arrowTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(arrowTapAction:)];
    [_arrow addGestureRecognizer:arrowTap];
    _arrow.backgroundColor = [UIColor whiteColor];
    [hearView addSubview:_arrow];
    [_arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hearView.mas_left).with.offset(WIDTH / 2 - 50);
        make.bottom.equalTo(hearView.mas_bottom).with.offset(0);
        make.height.equalTo(@(20));
        make.width.equalTo(@(100));
    }];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.tableHeaderView = hearView;
    [self.view addSubview:_tableView];
//    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0f];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
//        make.height.equalTo(@(HEIGHT - HEIGHT / 5 * 2));
    }];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[WMESongListCell class] forCellReuseIdentifier:@"songListCell"];
}
//动画上下
- (void)arrowTapAction:(UITapGestureRecognizer *)tap
{
    if (touchCount == 0) {
        [UIView animateWithDuration:1 animations:^{
//            self.arrow.frame = CGRectMake(WIDTH/ 2 - 50,HEIGHT -(HEIGHT - (HEIGHT - HEIGHT / 5 * 3)) - 64 - 20 , 100, 20);
            self.tableView.frame = CGRectMake(0, HEIGHT - 64 - 60 - (HEIGHT -(HEIGHT - (HEIGHT - HEIGHT / 5 * 3)) - 64), WIDTH, (HEIGHT -(HEIGHT - (HEIGHT - HEIGHT / 5 * 3)) - 64) + 60);
            _arrow.text = @"↑";
        }];
        touchCount = 1;
    }else{
        [UIView animateWithDuration:1 animations:^{
//            self.arrow.frame = CGRectMake(WIDTH/ 2 - 50,(HEIGHT - 64 - 40) - 20, 100, 20);
            self.tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 64);
            _arrow.text = @"↓";
        }];
        touchCount = 0;
    }
}
- (void)data
{
    self.dataArr = [NSMutableArray array];
     NSString *str = [@"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.diy.gedanInfo&from=ios&listid=6464&version=5.5.6&from=ios&channel=appstore&operator=1" stringByReplacingOccurrencesOfString:@"listid=6464" withString:[NSString stringWithFormat:@"listid=%@",_model.listid]];
    [WMEAFNetworking GETNetWithUrl:str body:nil headerFile:nil response:WMEJOSN success:^(id result) {
        NSArray *arr = result[@"content"];
        self.dataArr = [WMESongListModel modelHandlerWithArray:arr];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count) {
        WMESongListCell *songListCell = [_tableView dequeueReusableCellWithIdentifier:@"songListCell" forIndexPath:indexPath];
        WMESongListModel *model = _dataArr[indexPath.row];
        songListCell.model = model;
        return songListCell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count) {
        WMESongListModel *model = _dataArr[indexPath.row];
        NSString *str = [@"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.getInfos&ts=1461321078&songid=7330118&nw=2&l2p=218.9&lpb=320000&ext=MP3&format=json&from=ios&usup=1&lebo=0&aac=0&ucf=4&vid=&res=1&e=qfJshuUl8FS63N7L4gV2HAP%2FtWo%2F43nPR3pY%2FvY8iAo%3D&version=5.5.6&from=ios&channel=appstore&operator=1" stringByReplacingOccurrencesOfString:@"songid=7330118" withString:[NSString stringWithFormat:@"songid=%@", model.song_id]];
        [WMEAFNetworking GETNetWithUrl:str body:nil headerFile:nil response:WMEJOSN success:^(id result) {
            WMEPlayerViewController *playerVC = [[WMEPlayerViewController alloc] init];
            NSMutableArray *arr = [NSMutableArray array];
            arr = [WMEPlayerModel modelHandlerWithArray:result[@"songurl"][@"url"]];
            playerVC.listArr = arr;
            playerVC.dic = result;
            playerVC.songArr = _dataArr;
            playerVC.index = indexPath.row;
            NSString *strURL = result[@"songinfo"][@"lrclink"];
            [NetwrokHandler GETWithURL:strURL completion:^(id result) {
                NSLog(@"%@", result);
                playerVC.str = result;
                [self presentViewController:playerVC animated:YES completion:^{
                    NSLog(@"模态成功");
                }];
            }];
        } failure:^(NSError *error) {
            NSLog(@"网络请求错误");
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application,  you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
